package school;

import java.util.*;
import java.lang.*;
import java.io.*;

class College {
    public int node;
    public String name;

    public College(int node, String name) {
        this.node = node;
        this.name = name;
    }

    public int getNode() {
        return node;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "College{" +
                "node=" + node +
                ", name='" + name + '\'' +
                '}';
    }
}
public class Teacher {
    public static Teacher[] teachers = new Teacher[10];
    int n = 0;
    public int node;
    public String name;
    public String sex;
    public String birth;
    public College college;
    public void judge() throws IOException {
        String str;
        Scanner scanner = new Scanner(System.in);
        if(n == 0) {
            System.out.print("你还没有录入任何信息，是否录入(YES/NO):");
            str = scanner.next();
            while(str.charAt(0) != 'Y' && str.charAt(0) != 'N') {
                System.out.print("输入有误，请重新输入:");
                str = scanner.next();
            }
            if(str.charAt(0) == 'Y') {
                this.add();
            }
            if(str.charAt(0) == 'N') {
                this.menu();
            }
        }
    }
    public void add()throws IOException {
        String str,str1,str2;
        String temp;
        int num;
        int t = 1;
        FileWriter fileWriter = new FileWriter("E:/启迪/1/Teachers.txt",true);
        BufferedReader bufferedReader = new BufferedReader(new FileReader("E:/启迪/1/Teachers.txt"));
        temp = bufferedReader.readLine();
        if(temp == null) {
            fileWriter.write("录入的教职工信息列表\n教工编号 姓名 性别 出生年月 所在学院\n");
        }
        Scanner in = new Scanner(System.in);
        while(t == 1) {
            System.out.print("请输入教工编号:");
            num = in.nextInt();
            for(int i = 0;i < n;i++) {
                while(teachers[i].node==num) {
                    System.out.println("已存在此编号，请重新输入");
                    System.out.print("请输入编号:");
                    num = in.nextInt();
                }
            }
            teachers[n].node = num;
            str2 = String.valueOf(num);
            fileWriter.write(str2+" ");
            System.out.print("请输入教师姓名:");
            teachers[n].name = in.next();
            fileWriter.write(teachers[n].name+" ");
            System.out.print("请输入教师性别:");
            teachers[n].sex = in.next();
            fileWriter.write(teachers[n].sex+" ");
            System.out.print("请输入教师出生年月:");
            teachers[n].birth = in.next();
            fileWriter.write(teachers[n].birth+" ");
            System.out.print("请输入教师所在学院:");
            teachers[n].college = new College(in.nextInt(),in.next());
            fileWriter.write(teachers[n].college.toString()+"\r");
            n++;
            System.out.println();
            System.out.print("是否继续添加(YES/NO):");
            str = in.next();
            while(str.charAt(0) != 'N' && str.charAt(0) != 'Y') {
                System.out.print("输入有误，请重新输入:");
                str = in.next();
            }
            if(str.charAt(0) == 'N') {
                break;
            }
        }
        fileWriter.close();
        System.out.println();
        System.out.print("是否返回主菜单(YES/NO):");
        str1 = in.next();
        while(str1.charAt(0) != 'Y' && str1.charAt(0) != 'N') {
            System.out.print("输入有误，请重新输入:");
            str1 = in.next();
        }
        if(str1.charAt(0) == 'Y') {
            this.menu();
        }
        if(str1.charAt(0) == 'N') {
            System.out.println("正在退出...谢谢使用!");
            fileWriter.close();
            in.close();
            System.exit(0);
        }
    }
    public void show() throws IOException {
        this.judge();
        System.out.println("本次操作共录入" + n + "位教师!");
        System.out.println("你录入的教师信息如下:");
        System.out.println();
        System.out.println("编号 姓名 性别 出生年月 所在学院");
        for(int i = 0;i < n;i++) {
            System.out.println(teachers[i].node + " " + teachers[i].name + " " + teachers[i].sex + " " + teachers[i].birth + " " + teachers[i].college.toString());
        }
        System.out.println("系统返回主菜单!");
        menu();
    }
    public void delete() throws IOException {
        judge();
        int t = 0,k = 0;
        int num;
        String str;
        Scanner pin = new Scanner(System.in);
        System.out.print("请输入要删除的编号:");
        num = pin.nextInt();
        for(int i = 0;i < n;i++) {
            if(teachers[i].node == num) {
                k = 1;
                t = i;
            }
        }
        if(k == 0) {
            System.out.println("对不起！你要删除的编号不存在！");
            System.out.println("系统将返回主菜单！");
            menu();
        }
        if(k == 1) {
            System.out.println("你要删除的教师信息如下:");
            System.out.println("编号\t姓名\t性别\t出生年月\t所在学院");
            System.out.println(teachers[t].node + " " + teachers[t].name + " " + teachers[t].sex + " " + teachers[t].birth + " " + teachers[t].college.toString());
            System.out.println();
            System.out.print("你确定要删除(YES/NO):");
            str = pin.next();
            while(str.charAt(0) != 'Y' && str.charAt(0) != 'N') {
                System.out.print("输入有误，请重新输入:");
                str = pin.next();
            }
            if(str.charAt(0) == 'N') {
                System.out.println();
                System.out.println("系统返回主菜单！");
                menu();
            }
            if(str.charAt(0) == 'Y') {
                for(int j = t;j < n - 1;j++) {
                    teachers[j]=teachers[j + 1];
                }
                n--;
                System.out.println("数据成功删除!");
                System.out.println("系统返回主菜单!");
                menu();
            }
        }
    }
    public void look() throws IOException {
        FileReader fileReader=new FileReader("E:/启迪/1/Teachers.txt");
        int a = fileReader.read();
        while(a != -1) {
            char c = (char) a;
            System.out.print(c);
        }
        System.out.println();
        fileReader.close();
        System.out.println("系统返回主菜单！");
        System.out.println();
        menu();
    }
    public void modif() throws IOException {
        judge();
        int t = 0,k = 0,c = 1;
        int num2,num3,moi;
        String str1,str2,str3;
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要修改的编号:");
        num2 = scanner.nextInt();
        for(int i = 0;i < n;i++) {
            if(teachers[i].node==num2) {
                k = 1;
                t = i;
            }
        }
        if(k == 0) {
            System.out.println("对不起！你要修改的编号不存在！");
            System.out.println("系统将返回主菜单！");
            menu();
        }
        if(k == 1) {
            System.out.println("你要修改的学生信息如下:");
            System.out.println("编号\t姓名\t性别\t出生年月\t所在学院");
            System.out.println(teachers[t].node + " " + teachers[t].name + " " + teachers[t].sex + " " + teachers[t].birth + " " + teachers[t].college.toString());
            System.out.println();
            System.out.print("你确定要修改(YES/NO):");
            String str = scanner.next();
            while(str.charAt(0) != 'Y'&& str.charAt(0) != 'N') {
                System.out.print("输入有误，请重新输入:");
                str = scanner.next();
            }
            if(str.charAt(0) == 'N') {
                System.out.println();
                System.out.println("系统返回主菜单！");
                menu();
            }
            while(c == 1) {
                if(str.charAt(0) == 'Y') {
                    System.out.println("****************************************");
                    System.out.println("*****          1.修改教师编号       *****");
                    System.out.println("*****          2.修改姓名          *****");
                    System.out.println("*****          3.修改性别          *****");
                    System.out.println("*****          4.修改出生年月       *****");
                    System.out.println("*****          5.修改所在学院       *****");
                    System.out.println("****************************************");
                    System.out.print("请选择:");
                    moi = scanner.nextInt();
                    switch(moi) {
                        case 1:
                            System.out.print("请输入新的编号:");
                            num3 = scanner.nextInt();
                            teachers[t].node = num3;
                            break;
                        case 2:
                            System.out.print("请输入新的姓名:");
                            str1 = scanner.next();
                            teachers[t].name = str1;
                            break;
                        case 3:
                            System.out.print("请输入新的性别:");
                            str2 = scanner.next();teachers[t].sex=str2;break;
                        case 4:
                            System.out.print("请输入新的出生年月:");
                            str3 = scanner.next();
                            teachers[t].birth = str3;
                            break;
                        case 5:
                            System.out.print("请输入新的学院信息:");
                            int a = scanner.nextInt();
                            str3 = scanner.next();
                            teachers[t].college.setNode(a);
                            teachers[t].college.setName(str3);
                            break;
                    }
                    System.out.println("数据已成功修改！");
                }
                System.out.print("是否继续修改(YES/NO):");
                str = scanner.next();
                while(str.charAt(0) != 'Y' && str.charAt(0) != 'N') {
                    System.out.print("输入有误，请重新输入:");
                    str = scanner.next();
                }
                if(str.charAt(0) == 'N') {
                    break;
                }
            }
        }
        System.out.println();
        System.out.println("系统返回主菜单！");
        menu();
    }
    public void menu() throws IOException {
        System.out.println("**********学生信息管理系统功能表***********");
        System.out.println("*****          1.增加             *****");
        System.out.println("*****          2.修改             *****");
        System.out.println("*****          3.删除             *****");
        System.out.println("*****          4.查看             *****");
        System.out.println("*****          5.显示             *****");
        System.out.println("*****          0.退出             *****");
        System.out.println("***************************************");
        System.out.print("请选择(0~5):");
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNextInt()){
            int a = scanner.nextInt();
            while(a < 0 || a > 5) {
                System.out.print("输入超出范围，请重新输入:");
                a = scanner.nextInt();
            }
            switch(a) {
                case 1:
                    add();
                    break;
                case 2:
                    modif();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    look();
                    break;
                case 5:
                    show();
                    break;
                case 0:
                    System.out.println("系统已退出!");
                    System.exit(0);
                    break;
            }
        }
    }
    public static void main(String[] args) throws IOException{
        Teacher teacher=new Teacher();
        for(int i = 0;i < 10;i++) {
            teachers[i]=new Teacher();
        }
        teacher.menu();
    }
}